class CreateSingle {
  constructor(name) {
    this.name = name;
    this.instance = null;
  }
  getInstance(name) {
    if (!this.instance) {
      this.instance = new CreateSingle(name);
    }
    return this.instance;
  }
}

let createSingle = new CreateSingle();
var aa = createSingle.getInstance("111");
var bb = createSingle.getInstance("222");

console.log(bb.name);

// 闭包single

function CreateSingleFn(fn) {
  let instance;
  return function single() {
    if (!instance) {
      return (instance = new fn(...arguments));
    } else {
      return instance;
    }
  };
}

function Test(name) {
  this.name = name;
}

let aaf = new CreateSingleFn(Test);

console.log(aaf("lhf"));
console.log(aaf("lhf111"));
